package com.panfeng.xcloud.boss.provider.member.utils;

import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * token请求工具
 *
 * @author xiaobo
 * @version 1.0
 * @since 2018-12-28
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TokenRequestUtils {

    /**
     * 请求认证中心 token endpoint url
     *
     * @param tokenUrl
     * @param tokenParamMap
     * @param request
     * @return
     */
    public static String requestToken(String tokenUrl, Map<String, Object> tokenParamMap, HttpServletRequest request){
        //Header[] headers = HttpHeader.custom().contentType(HttpHeader.Headers.APP_FORM_URLENCODED).authorization(request.getHeader(HttpHeaders.AUTHORIZATION)).build();
        Header[] headers = HttpHeader.custom().contentType(HttpHeader.Headers.APP_FORM_URLENCODED).build();
        HttpConfig config = HttpConfig.custom().headers(headers).url(tokenUrl).map(tokenParamMap);
        String tokenInfoString = "";
        try {
            tokenInfoString = HttpClientUtil.post(config);
            log.info(">>> token信息={} <<<",tokenInfoString);
        } catch (HttpProcessException e) {
            log.error("requestToken={}", e.getMessage(), e);
            e.printStackTrace();
        }
        return tokenInfoString;
    }

    /**
     * 刷新token
     *
     * @param tokenUrl
     * @param clientId
     * @param clientSecret
     * @param refreshToken
     * @param request
     * @return
     */
    public static String refreshToken(String tokenUrl,String clientId, String clientSecret, String refreshToken, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>(2);
        map.put("grant_type", "refresh_token");
        map.put("client_id", clientId);
        map.put("client_secret", clientSecret);
        map.put("refresh_token", refreshToken);
        String tokenInfoString = requestToken(tokenUrl, map, request);
        return tokenInfoString;
    }

    /**
     * 获取token
     *
     * @param tokenUrl
     * @param clientId
     * @param clientSecret
     * @param userName
     * @param request
     * @return
     */
    public static String loginToken(String tokenUrl,String clientId, String clientSecret,String userName, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>(5);
        map.put("grant_type", "password");
        map.put("client_id", clientId);
        map.put("client_secret", clientSecret);
        map.put("openId", userName);
        String tokenInfoString = requestToken(tokenUrl, map, request);
        return tokenInfoString;
    }

    /**
     * 验证码获取token
     *
     * @param tokenUrl
     * @param clientId
     * @param clientSecret
     * @param mobileNo
     * @param verifyCode
     * @param request
     * @return
     */
    public static String verifyCodeLogin(String tokenUrl,String clientId, String clientSecret,String mobileNo, String verifyCode, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>(6);
        map.put("grant_type", "mobile");
        map.put("scope", "server");
        map.put("mobile", mobileNo);
        map.put("verifyCode", verifyCode);
        map.put("clientId", clientId);
        map.put("clientSecret", clientSecret);
        String tokenInfoString = requestToken(tokenUrl, map, request);
        return tokenInfoString;
    }

    /**
     * 密码获取token
     *
     * @param tokenUrl
     * @param clientId
     * @param clientSecret
     * @param accountNumber
     * @param password
     * @param request
     * @return
     */
    public static String pwdLogin(String tokenUrl,String clientId, String clientSecret,String accountNumber, String password, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>(6);
        map.put("grant_type", "password");
        map.put("scope", "server");
        map.put("accountNumber", accountNumber);
        map.put("password", password);
        map.put("clientId", clientId);
        map.put("clientSecret", clientSecret);
        String tokenInfoString = requestToken(tokenUrl, map, request);
        return tokenInfoString;
    }

}
